/*
 *  config.cpp
 *
 *  Copyright (c) 2008 by Artyom Smirnov <smirnoffjr@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <boost/algorithm/string.hpp>

#include "config.hpp"
#include "string_utils.hpp"

namespace balg = boost::algorithm;

Configuration::Configuration()
{
    //
    // read configuration file and fill up variables
    //

    char line[1024];

    std::string home_dir = getenv("HOME");
    std::string conf_file = home_dir + "/" + CONFIG_FILE;

    std::ifstream conf;
    conf.open(conf_file.c_str(), std::ifstream::in);
    while(conf.good())
    {
        conf.getline(line, sizeof(line));
        std::string s = line;
        string_list l;
        balg::split(l, s, balg::is_any_of("="));
        if (l.size() >= 2)
        {

            std::string name = l[0];
            balg::trim(name);

            std::string val = l[1];
            balg::trim(val);
            if (name == "ignore")
            {
                string_list list;
                balg::split(list, val, balg::is_any_of(","));
                for (string_list::iterator item = list.begin(); item != list.end(); item++)
                {
                    balg::trim(*item);
                    ignore_list.push_back(*item);
                }
            }
        }
    }
    conf.close();
}

string_list Configuration::GetIgnoreList()
{
    //
    // return ignored packages list
    //

    return ignore_list;
}
